import { _decorator, Component, debug, JsonAsset, Node, Prefab, resources, SpriteFrame } from 'cc';
import { Singleton } from './Singleton';
import { Loader } from './LoadManger';
import { load } from '../ui/Load';
const { ccclass, property } = _decorator;

export enum PrefabKEY {
    mainUI = "main",
    gameChoose = "chooseGame",
    gameSuccess = "gameSuccess",
    gameLose = "loseGame",
    levelChoose = "chooseLevel",
    prompt = "prompt"
}

export class ResManger extends Singleton<ResManger> {
    private _configJson: JsonAsset = null;
    private _iTotal: number = 0;
    private _iCurLoader: number = 0;
    private _bLoadLock: boolean = false;
    private _cLoadCallBack: Function = null;
    //private _prefabArr: string[] = [];
    private _monsterSpfMap: Map<string, SpriteFrame[]> = new Map();
    private _skillSpfMap: Map<string, SpriteFrame[]> = new Map();
    private _bulletSpfMap: Map<string, SpriteFrame> = new Map();
    //private _maxPrefab = 4;
    public InitResManger(back?: () => void) {
        this._bLoadLock = true;
        this._cLoadCallBack = back;
    }

    InitPrefabByName(fileName: string, back?: () => void) {
        Loader.LoadPrefab(fileName, (prefab) => {
            if (back) back();
        });
    }

    InitJson(back?: () => void) {
        let fileName = "aConfig";
        Loader.LoadJson(fileName, (json) => {
            this._configJson = json;
            if (back) back();
        });
    }

    public get configJson() {
        return this._configJson.json;
    }

    public set configJson(json: any) {
        this._configJson = json;
    }

    LoadCallBack() {
        this._iCurLoader++;
        if (this._bLoadLock == false) {
            console.warn("ResManger LoadCallBack _bLoadLock false");
            return;
        }
        if (this._cLoadCallBack == null) {
            console.error("ResManger loadCallBack null");
            return;
        }
        if (this._iTotal == 0) {
            console.error("ResManger LoadCallBack _iTotal 0");
            return;
        }
        if (this._iCurLoader == this._iTotal && this._bLoadLock) {
            this._bLoadLock = false
            this._cLoadCallBack();
            this._cLoadCallBack = null;
            this._iTotal = 0;
            this._iCurLoader = 0;
        }
    }

    public GetPrefabByKey(fileName: string, back: () => void) {
        if (!Loader.GetPrefab(fileName)) {
            Loader.LoadPrefab(fileName, () => {
                if (back) back();
            })
        } else {
            if (back) back();
        }
    }

    public GetUiPrefabByKey(fileName: string, back: () => void) {
        if (!Loader.GetUiPrefab(fileName)) {
            // this._prefabArr.push(fileName);
            // if (this._prefabArr.length > this._maxPrefab) {
            //     Loader.ReleaseUiPrefab(this._prefabArr.shift());
            // }
            Loader.LoadUiPrefab(fileName, () => {
                if (back) back();
            })
        } else {
            if (back) back();
        }
    }

    public GetMonsterAniSpf(fileName: string, back: (spf: SpriteFrame[]) => void) {
        let res = this._monsterSpfMap.get(fileName);
        if (res) {
            back(res);
            return;
        }
        Loader.LoadMonsterImageDir(fileName, (arr) => {
            this._monsterSpfMap.set(fileName, arr);
            back(arr);
        })

    }

    public GetSkillAniSpf(fileName: string, back: (spf: SpriteFrame[]) => void) {
        let res = this._skillSpfMap.get(fileName);
        if (res) {
            back(res);
            return;
        }
        Loader.LoadSkillImageDir(fileName, (arr) => {
            this._skillSpfMap.set(fileName, arr);
            back(arr);
        })

    }

    public GetBulletSpf(fileName: string, back: (spf: SpriteFrame) => void) {
        let res = this._bulletSpfMap.get(fileName);
        if (res) {
            back(res);
            return;
        }
        Loader.LoadBulletImage(fileName, (spf) => {
            this._bulletSpfMap.set(fileName, spf);
            back(spf);
        })

    }

    public PreLoadResByGame(game) {
    }

    public ReleaseResByGame(game) {
    }
}

export var resManger: ResManger = ResManger.Instance<ResManger>();
